import { _decorator, Component, EditBox, Button, Label, sys } from 'cc';
import {Server} from "./lib/server";
import {pb, api} from "./lib/apiProto";
const { ccclass, property } = _decorator;

// const pb = pb2.pb;
@ccclass
export class Main extends Component {

    @property({
        type: EditBox,
        tooltip: '用户名',
    })
    userName: EditBox

    @property({
        type: EditBox,
        tooltip: '消息',
    })
    message: EditBox

    @property({
        type: Button,
        tooltip: '消息发送',
    })
    sendButton: Button

    @property({
        type: Label,
        tooltip: '消息展示',
    })
    printer: Label
    start() {
        console.log('start main');
        Server.init('localhost', 12345, '/')
    }

    // update(deltaTime: number) {
    //
    // }

    sendMessage() {
        const username = this.userName.string;
        const msg = this.message.string;
        console.log('username', username, 'message', msg, pb);
        // 先加入房间
        api.playerLogin({
            touristId: "demo",
            loginType: pb.LoginType.Tourist,
            platform: sys.platform
        }, () => {
            console.log("login success")
        })
        // Server.request(routersKey.playerLogin, pb.PlayerLoginReq.create({
        //     touristId: "demo",
        //     loginType: pb.LoginType.Tourist,
        //     platform: sys.platform,
        // }), () => {
        //     console.log('join success');
        // })
        // Server.request('room.message', {name: username, content: msg}, null)
        // // 服务器推送的消息
        // Server.s.on('onMessage', msgBody => {
        //     this.printer.string += `\n${msgBody.name}: ${msgBody.content}`;
        // })
    }
}


